<template>
  <div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="loginform">
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="输入您的手机号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="输入您的密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="pwd"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <div class="f-14 aggren">
        登录即表示您已同意《<span class="c_primary">服务条款</span>》
      </div>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          class="f-16 w_full loginbtn"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">立即登录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
      <div class="checkbox flexbetween mb-20">
        <el-checkbox v-model="loginForm.rememberMe">下次自动登录</el-checkbox>
        <div class="commhover c_primary">忘记密码</div>
      </div>
      <div class="other_top f-14 c_000">
        <span class="other_text">第三方登录</span>
      </div>
      <div class="flexcenter othericon mt-15">
        <img src="@/assets/images/qq.png" alt="" class="commhover" />
      </div>
    </el-form>
  </div>
</template>

<script>
import Cookies from "js-cookie";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods:{
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
          //  Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set("password", this.loginForm.password, { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            // if (this.captchaEnabled) {
            //   this.getCode();
            // }
          });
        }
      });
      // this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
    }
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/login.scss';
</style>
